package com.wdl.modules.myhouse.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author 昏昏的君
 */
@Data
@ApiModel("全局统一返回结果")
public class Result<T>{
    private static final long serialVersionUID = 1L;
    /**
     * 编码：0表示成功，其他值表示失败
     */
    @ApiModelProperty(value = "编码：200表示成功，其他值表示失败")
    private int code = Code.SUCCESS.getCode();
    /**
     * 消息内容
     */
    @ApiModelProperty(value = "消息内容")
    private String msg = Code.SUCCESS.getMessage();
    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    public Result<T> ok() {
        return this;
    }

    public Result<T> ok(T data) {
        this.setData(data);
        return this;
    }

    public Result<T> setData(T data) {
        this.data=data;
        return this;
    }

    public boolean success(){
        return code == Code.SUCCESS.getCode();
    }

    public Result<T> error() {
        this.code=Code.INTERNAL_ERROR.getCode();
        this.msg=Code.INTERNAL_ERROR.getMessage();
        return this;
    }

    public Result<T> error(Integer code,String msg) {
        this.code=code;
        this.msg=msg;
        return this;
    }

    public Result<T> error(String msg) {
        this.code=Code.COMMON_ERROR.getCode();
        this.msg=msg;
        return this;
    }

}
